package com.techysky.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.techysky.entity.DataModule;
import com.techysky.entity.PublicModule;
import com.techysky.repository.DataModuleRepository;
import com.techysky.repository.PublicModuleRepository;
import com.techysky.util.Tree;

@Service("treeService")
public class TreeServiceImpl implements com.techysky.service.TreeService {

	private static final String IS_PARENT = "isParent";
	private static final String PARENT_ID = "parentId";
	private static final String FALSE = "false";
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;	
	@Autowired
	private transient PublicModuleRepository publicModuleRepository;
	@Autowired
	private transient DataModuleRepository dataModuleRepository;
	@Override
	public Object initTree(String id,HttpServletRequest request) {

		return null;
	}
	@Override
	public Object initTree(String id, String fitName,String type,HttpServletRequest request) {
		
		return this.initProjectTreeAll(id, fitName,type);
	}
	
	public List<Tree> initProjectTree(String id) {
		List<Tree> trees=new  ArrayList<>();
		Iterable<PublicModule> pms=this.publicModuleRepository.findByProjectIdAndParentIdIsNull(id);
		for (PublicModule pm : pms) {
			Tree tree=new Tree();
			tree.setId(pm.getId());
			tree.setNodeId(pm.getId());
			tree.setName(pm.getTitle());
			tree.setIsParent("true");
			tree.setIsLast(FALSE);
			tree.setType("pm");
			tree.setPd("one");
			tree.setIcon("");
			trees.add(tree);
			}
		return trees;
	}
	
	public List<Tree> initProjectTreeAll(String id,String fitName,String type) {
		List<Tree> trees=new  ArrayList<>();
		Iterable<PublicModule> pms=this.publicModuleRepository.findByProjectIdAndParentIdIsNull(id);
		for (PublicModule pm : pms) {
			Tree tree=new Tree();
			tree.setId(pm.getId());
			tree.setNodeId(pm.getId());
			tree.setName(pm.getTitle());
			tree.setType("pm");
			tree.setPd("one");
			tree.setIcon("");
			int num=getChildren(pm.getId());
			if(num>=1){
				tree.setIsParent("true");
				tree.setIsLast(FALSE);
				trees.add(tree);
				this.getPmChildren(trees, pm.getId(),fitName,type);
			}else{
				int num2=getChildren2(pm.getId());
				if(num2>=1){
					tree.setIsParent("true");
					tree.setIsLast("true");	
					trees.add(tree);
					this.getDmChildren(trees, pm.getId(),fitName,type);
				}
			}
			}
		return trees;
	}
	
	public List<Tree> getPmChildren(List<Tree> trees,String parentId) {
			List<PublicModule> publicModules=this.publicModuleRepository.findByParentId(parentId);
			//
			for (PublicModule publicModule : publicModules) {
				Tree tree = new Tree();
				tree.setId(publicModule.getId());
				tree.setpId(publicModule.getParentId());
				tree.setPid1(publicModule.getParentId());
				tree.setName(publicModule.getTitle());
				tree.setTarget(publicModule.getId());
				tree.setType("pm");
				int num=getChildren(publicModule.getId());
				if(num>=1){
					tree.setIsParent("true");
					tree.setIsLast(FALSE);
					trees.add(tree);
					this.getPmChildren(trees, publicModule.getId());
				}else{
					int num2=getChildren2(publicModule.getId());
					if(num2>=1){
						tree.setIsParent("true");
						tree.setIsLast("true");	
						trees.add(tree);
						this.getDmChildren(trees, publicModule.getId());
					}
				}
			}
			this.getDmChildren(trees, parentId);
		return trees;
	}
	public List<Tree>  getDmChildren(List<Tree> trees,String parentId) {
		
		
			List<DataModule> dataModules=this.dataModuleRepository.findByPublicModuleId(parentId);
			for (DataModule dataModule : dataModules) {
				Tree tree = new Tree();
				tree.setId(dataModule.getId());
				tree.setpId(dataModule.getPublicModuleId());
				tree.setPid1(dataModule.getPublicModuleId());
				tree.setName(dataModule.getTitle());
				tree.setCode(dataModule.getCode());
				tree.setDmCode(dataModule.getDmCode());
				tree.setType("dm");
				tree.setUrl(dataModule.getUrl());
				tree.setTarget("home");
				tree.setIsParent(FALSE);
				trees.add(tree);
				}		
		
		
		return trees;
	}
	
	
	/**
	 * 适用性过滤
	 * @param id
	 * @param fitName
	 * @param request
	 * @return
	 */
	public List<Tree> getPmChildren(List<Tree> trees,String parentId,String fitName,String type) {
		List<PublicModule> publicModules=this.publicModuleRepository.findByParentId(parentId);
		for (PublicModule publicModule : publicModules) {
			Tree tree = new Tree();
			tree.setId(publicModule.getId());
			tree.setpId(publicModule.getParentId());
			tree.setPid1(publicModule.getParentId());
			tree.setName(publicModule.getTitle());
			tree.setTarget(publicModule.getId());
			tree.setType("pm");
			int num=getChildren(publicModule.getId());
			if(num>=1){
				tree.setIsParent("true");
				tree.setIsLast(FALSE);
				trees.add(tree);
				this.getPmChildren(trees, publicModule.getId(),fitName,type);
			}else{
				int num2=getChildren2(publicModule.getId());
				if(num2>=1){
					tree.setIsParent("true");
					tree.setIsLast("true");	
					trees.add(tree);
					this.getDmChildren(trees, publicModule.getId(),fitName,type);
				}
			}
		}
		this.getDmChildren(trees, parentId,fitName,type);
	return trees;
}
	/**
	 * 适用性过滤
	 * @param id
	 * @param fitName
	 * @param request
	 * @return
	 */
	public List<Tree>  getDmChildren(List<Tree> trees,String parentId,String fitName,String type) {
		
		List<DataModule> dataModules=this.dataModuleRepository.findByPublicModuleId(parentId);
		if(fitName!=""&&fitName!=null){
			for (DataModule dataModule : dataModules) {
				if("all".equals(dataModule.getFitName())||fitName.contains(dataModule.getFitName())){
					extract1(trees, type, dataModule);					
				}else{
					continue;
				}
				}	
		}else{
			for (DataModule dataModule : dataModules) {
				extract1(trees, type, dataModule);
				}	
		}

	
	return trees;
}
	private void extract1(List<Tree> trees, String type, DataModule dataModule) {
		Tree tree = new Tree();
		tree.setId(dataModule.getId());
		tree.setpId(dataModule.getPublicModuleId());
		tree.setPid1(dataModule.getPublicModuleId());
		tree.setName(dataModule.getTitle());
		tree.setCode(dataModule.getCode());
		tree.setDmCode(dataModule.getDmCode());
		tree.setType("dm");
		if(null!=type&&!"exam".equals(type)){
		tree.setUrl(dataModule.getUrl());
		tree.setTarget("home");
		}
		tree.setIsParent(FALSE);
		trees.add(tree);
	}
	@Override
	public Object getChildrenTrees(HttpServletRequest request) {
		List<Tree> trees=new ArrayList<>();
		if("pm".equals(request.getParameter("type"))){
			List<PublicModule> publicModules=this.publicModuleRepository.findByParentId(request.getParameter(PARENT_ID));
			for (PublicModule publicModule : publicModules) {
				extract1(trees, publicModule);
			}
		}else if("dm".equals(request.getParameter("type"))){
			List<DataModule> dataModules=this.dataModuleRepository.findByPublicModuleId(request.getParameter(PARENT_ID));
			for (DataModule dataModule : dataModules) {
				Tree tree = new Tree();
				tree.setId(dataModule.getId());
				tree.setpId(dataModule.getPublicModuleId());
				tree.setPid1(dataModule.getPublicModuleId());
				tree.setName(dataModule.getTitle());
				tree.setCode(dataModule.getCode());
				tree.setDmCode(dataModule.getDmCode());
				tree.setType("dm");
				tree.setUrl(dataModule.getUrl());
				tree.setTarget("home");
				tree.setIsParent(FALSE);
				trees.add(tree);
				}		
		}
		
		return trees;
	}
	private void extract1(List<Tree> trees, PublicModule publicModule) {
		Tree tree = new Tree();
		tree.setId(publicModule.getId());
		tree.setpId(publicModule.getParentId());
		tree.setPid1(publicModule.getParentId());
		tree.setName(publicModule.getTitle());
		tree.setTarget(publicModule.getId());
		tree.setType("pm");
		int num=getChildren(publicModule.getId());
		if(num>=1){
			tree.setIsParent("true");
			tree.setIsLast(FALSE);
		}else{
			int num2=getChildren2(publicModule.getId());
			if(num2>=1){
				tree.setIsParent("true");
				tree.setIsLast("true");				
			}
		}
		trees.add(tree);
	}
	public Object getChildrenTrees2(HttpServletRequest request) {
		List<Map<String,Object>> trees=new ArrayList<>();
		if("pm".equals(request.getParameter("type"))){
			List<PublicModule> publicModules=this.publicModuleRepository.findByParentId(request.getParameter(PARENT_ID));
			for (PublicModule publicModule : publicModules) {
				extract2(trees, publicModule);
			}
		}else if("dm".equals(request.getParameter("type"))){
			List<DataModule> dataModules=this.dataModuleRepository.findByPublicModuleId(request.getParameter(PARENT_ID));
			for (DataModule dataModule : dataModules) {
				Map<String,Object> tree=new HashMap<>();
				tree.put("id", dataModule.getId());
				tree.put("pId", dataModule.getPublicModuleId());
				tree.put("pid", dataModule.getPublicModuleId());
				tree.put("name", dataModule.getTitle());
				tree.put("code", dataModule.getCode());
				tree.put("url", dataModule.getUrl());
				tree.put("type", "dm");
				tree.put("target","home");
				tree.put(IS_PARENT, FALSE);
				}		
		}
		
		return trees;
	}
	private void extract2(List<Map<String, Object>> trees, PublicModule publicModule) {
		Map<String,Object> tree=new HashMap<>();
		tree.put("id", publicModule.getId());
		tree.put("pId", publicModule.getParentId());
		tree.put("pid", publicModule.getParentId());
		tree.put("name", publicModule.getTitle());
		tree.put("target", publicModule.getId());
		tree.put("type", "pm");
		
		int num=getChildren(publicModule.getId());
		if(num>=1){
			tree.put(IS_PARENT, "true");
			tree.put("isLast", FALSE);
		}else{
			int num2=getChildren2(publicModule.getId());
			if(num2>=1){
				tree.put(IS_PARENT, "true");
				tree.put("isLast", "true");			
			}
		}
		trees.add(tree);
	}
	public int getChildren(String parentid) {
		List<PublicModule> pmList=this.publicModuleRepository.findByParentId(parentid);
		int num=0;
		if(!pmList.isEmpty()){
			return pmList.size();
		}
		else
			return num;
	}
	public int getChildren2(String parentid) {
		List<DataModule> dmList=this.dataModuleRepository.findByPublicModuleId(parentid);
		int num=0;
		if(!dmList.isEmpty()){
			return dmList.size();
		}
		else
			return num;
	}
	/**
	 * 获取某一pmID下所有的dm
	 */
	@Override
	public List<PublicModule> getAllDm(String parentId){
		List<PublicModule> allpmList=new  ArrayList<>();
		this.getPmChildrenAll(allpmList, parentId);
		return allpmList;
	}
	/**
	 * 获取某一pmID下所有的dm
	 */
	public List<PublicModule> getPmChildrenAll(List<PublicModule> allpmList,String parentId) {
		List<PublicModule> publicModules=this.publicModuleRepository.findByParentId(parentId);
		for (PublicModule publicModule : publicModules) {
			allpmList.add(publicModule);
			int num=getChildren(publicModule.getId());
			if(num>=1){
				this.getPmChildrenAll(allpmList, publicModule.getId());
			}
		}
	return allpmList;
}

}
